const express = require('express');
const validate = require('../../middleware/validate');
const recipeController = require('../../controllers/recipe.controller');
const recipeValidation = require('../../validators/recipe.validator');
const { auth, authorize } = require('../../middleware/auth');

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: Recipes
 *   description: 食谱管理
 */

/**
 * @swagger
 * /admin/recipes:
 *   get:
 *     summary: 获取食谱列表
 *     description: 管理员可以获取所有食谱列表。
 *     tags: [Recipes]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: name
 *         schema:
 *           type: string
 *         description: 食谱名称（模糊匹配）
 *       - in: query
 *         name: difficulty
 *         schema:
 *           type: string
 *           enum: [简单, 中等, 困难]
 *         description: 难度等级
 *       - in: query
 *         name: isVerified
 *         schema:
 *           type: boolean
 *         description: 是否已验证
 *       - in: query
 *         name: isPublished
 *         schema:
 *           type: boolean
 *         description: 是否已发布
 *       - in: query
 *         name: tags
 *         schema:
 *           type: string
 *         description: 标签（多个标签用逗号分隔）
 *       - in: query
 *         name: sortBy
 *         schema:
 *           type: string
 *         description: 排序字段（例如：createdAt:desc）
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *         default: 10
 *         description: 每页数量
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *         default: 1
 *         description: 页码
 *     responses:
 *       "200":
 *         description: OK
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 results:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Recipe'
 *                 page:
 *                   type: integer
 *                   example: 1
 *                 limit:
 *                   type: integer
 *                   example: 10
 *                 totalPages:
 *                   type: integer
 *                   example: 5
 *                 total:
 *                   type: integer
 *                   example: 48
 *       "401":
 *         $ref: '#/components/responses/Unauthorized'
 *       "403":
 *         $ref: '#/components/responses/Forbidden'
 */
router.get('/', validate(recipeValidation.getRecipes), recipeController.getRecipes);

router
  .route('/')
  .post(auth(), authorize(['manageContent']), validate(recipeValidation.validateRecipe), recipeController.createRecipe);

/**
 * @swagger
 * /admin/recipes/{id}:
 *   get:
 *     summary: 获取单个食谱详情
 *     description: 管理员可以获取单个食谱的详细信息。
 *     tags: [Recipes]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 食谱ID
 *     responses:
 *       "200":
 *         description: OK
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Recipe'
 *       "401":
 *         $ref: '#/components/responses/Unauthorized'
 *       "403":
 *         $ref: '#/components/responses/Forbidden'
 *       "404":
 *         $ref: '#/components/responses/NotFound'
 *
 *   patch:
 *     summary: 更新食谱
 *     description: 管理员可以更新食谱信息。
 *     tags: [Recipes]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 食谱ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               description:
 *                 type: string
 *               ingredients:
 *                 type: array
 *                 items:
 *                   type: object
 *               steps:
 *                 type: array
 *                 items:
 *                   type: string
 *               cookingTime:
 *                 type: number
 *               difficulty:
 *                 type: string
 *               tags:
 *                 type: array
 *                 items:
 *                   type: string
 *               isVerified:
 *                 type: boolean
 *               isPublished:
 *                 type: boolean
 *     responses:
 *       "200":
 *         description: OK
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Recipe'
 *       "401":
 *         $ref: '#/components/responses/Unauthorized'
 *       "403":
 *         $ref: '#/components/responses/Forbidden'
 *       "404":
 *         $ref: '#/components/responses/NotFound'
 *
 *   delete:
 *     summary: 删除食谱
 *     description: 管理员可以删除食谱。
 *     tags: [Recipes]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 食谱ID
 *     responses:
 *       "200":
 *         description: 删除成功
 *       "401":
 *         $ref: '#/components/responses/Unauthorized'
 *       "403":
 *         $ref: '#/components/responses/Forbidden'
 *       "404":
 *         $ref: '#/components/responses/NotFound'
 */
router
  .route('/:recipeId')
  .get(auth(), authorize(['manageContent']), recipeController.getRecipe)
  .patch(auth(), authorize(['manageContent']), recipeController.updateRecipe)
  .delete(auth(), authorize(['manageContent']), recipeController.deleteRecipe);

/**
 * @swagger
 * /admin/recipes/{id}/soft:
 *   delete:
 *     summary: 软删除食谱
 *     description: 管理员可以软删除食谱（标记为已删除但不物理删除）。
 *     tags: [Recipes]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 食谱ID
 *     responses:
 *       "200":
 *         description: 软删除成功
 *       "401":
 *         $ref: '#/components/responses/Unauthorized'
 *       "403":
 *         $ref: '#/components/responses/Forbidden'
 *       "404":
 *         $ref: '#/components/responses/NotFound'
 */
router
  .route('/:recipeId/soft')
  .delete(auth(), authorize(['manageContent']), recipeController.softDeleteRecipe);

/**
 * @swagger
 * /admin/recipes/{id}/restore:
 *   post:
 *     summary: 恢复已删除的食谱
 *     description: 管理员可以恢复已软删除的食谱。
 *     tags: [Recipes]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 食谱ID
 *     responses:
 *       "200":
 *         description: 恢复成功
 *       "401":
 *         $ref: '#/components/responses/Unauthorized'
 *       "403":
 *         $ref: '#/components/responses/Forbidden'
 *       "404":
 *         $ref: '#/components/responses/NotFound'
 */
router
  .route('/:recipeId/restore')
  .post(auth(), authorize(['manageContent']), recipeController.restoreRecipe);

module.exports = router; 